package com.bookhouse.controller;

import com.bookhouse.entity.Menu;
import com.bookhouse.service.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by CiusCheung on 16/4/21.
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Resource(name = "menuService")
    private MenuService menuService;


    @RequestMapping("getAllMenu")
    public String getAllMenu(HttpServletRequest request) {
        Set<Menu> sets = new HashSet<Menu>();
        Menu menu = menuService.get(Menu.class, 0);
        Set<Menu> levels1 = menu.getChildrenMenu();
        for (Menu level1 : levels1) {
            sets.add(level1);
            level1.setMenuSet(new HashSet<Menu>());
            Set<Menu> levels2 = level1.getChildrenMenu();
            for (Menu level2 : levels2) {
                level1.getMenuSet().add(level2);
            }
        }
        request.setAttribute("menuList", sets);
        return "menu";
    }

    @RequestMapping("getSite")
    public String getSite(String menuName, HttpServletRequest request) {
        if (menuName.equals("publisherEdit"))
            return "forward:/press/getAllPress";

        if (menuName.equals("bookEdit"))
            return "redirect:/book/getAllBook";

        if (menuName.equals("readerType"))
            return "redirect:/readerType/getAllReaderType";

        if (menuName.equals("editReader"))
            return "redirect:/reader/getAllReader";

        if (menuName.equals("borrowBook"))
            return "borrow";

        if (menuName.equals("returnBook"))
            return "redirect:/record/getAllRecord";

        if (menuName.equals("allRecord"))
            return "redirect:/record/getRecordList";

        if (menuName.equals("overdueRecord"))
            return "redirect:";

        if (menuName.equals("countRecord"))
            return "redirect:";

        if (menuName.equals("addAdmin"))
            return "redirect:";

        if (menuName.equals("editManager"))
            return "redirect:";

        if (menuName.equals("tool"))
            return "redirect:";

        if (menuName.equals(""))
            return "redirect:";

        if (menuName.equals(""))
            return "redirect:";

        else
            return "redirect:";

    }


}
